﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Text.RegularExpressions;
using System.Net.Mail;
using System.Net;
using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;

namespace SecurityDev
{
    public partial class PersonsForm : Form
    {
        public PersonsForm()
        {
            InitializeComponent();
        }

        private void CustomersForm_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = DS.outpost.person;
            dataGridView1.Columns["person_seq"].HeaderText = "Person ID";
            dataGridView1.Columns["person_name"].HeaderText = "Name";
            dataGridView1.Columns["person_lastname"].HeaderText = "Lastname";
            dataGridView1.Columns["person_adress"].HeaderText = "Adress";
            dataGridView1.Columns["person_phone"].HeaderText = "Phone";
            dataGridView1.Columns["person_email"].HeaderText = "Email";
            dataGridView1.Columns["person_photo"].HeaderText = "Photo";
            dataGridView1.Columns["person_is_customer"].HeaderText = "Is Customer";
        }

        private void DoneBtn_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.Cursor = Cursors.AppStarting;

            decimal person_seq = (decimal)dataGridView1.Rows[e.RowIndex].Cells["person_seq"].Value;
            
            PersonDetail personDetail = null;
            try
            {
                personDetail = new PersonDetail(person_seq);
            }
            catch (NullReferenceException ex)
            {
                MessageBox.Show(ex.Message);
            }
            if (personDetail != null)
            {
                personDetail.ShowDialog();
            }

            this.Cursor = Cursors.Default;
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 1)
            {
                changeBtn.Enabled = true;
                deleteBtn.Enabled = true;
            }
            else
            {
                changeBtn.Enabled = false;
                deleteBtn.Enabled = false;
            }
        }        

        private void addBtn_Click(object sender, EventArgs e)
        {
            InitializePerson dialog = new InitializePerson();
            dialog.ShowDialog();
        }

        private void changeBtn_Click(object sender, EventArgs e)
        {
            InitializePerson dialog = new InitializePerson((decimal)dataGridView1.SelectedRows[0].Cells[0].Value);
            dialog.ShowDialog();
        }

        private void deleteBtn_Click(object sender, EventArgs e)
        {
            OutpostDS.personRow r = DS.outpost.person.FindByperson_seq((decimal)dataGridView1.SelectedRows[0].Cells[0].Value);
            foreach (var obj_perR in r.GetChildRows("op_ibfk_1"))
            {
                obj_perR.Delete();
            }
            r.Delete();
            try
            {
                DS.object_personAdapter.Update(DS.outpost.object_person);
                DS.personAdapter.Update(DS.outpost.person);            
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

    }
}
